home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / DYN401.ZIP / class / btree.d < prev    next >
Text File  |  1996-08-25  |  5KB  |  245 lines

  1.  
  2.  
  3.  
  4. /*                                      
  5.  *
  6.  *      Copyright (c) 1993-1996 Algorithms Corporation
  7.  *      3020 Liberty Hills Drive
  8.  *      Franklin, TN 37067
  9.  *
  10.  *      ALL RIGHTS RESERVED.
  11.  *
  12.  *      
  13.  *      
  14.  */
  15.  
  16.  
  17.  
  18.  
  19. defclass  BTree {
  20.     iNodes;
  21.     long    iNum;        //  total number of keys entered
  22.     ifun    iCmpFun;
  23.     CRITICALSECTION    iCS;    /*  in support of native threads  */
  24. };
  25.  
  26. imeth    ofun    gSetFunction(int (*fun)())
  27. {
  28.     ofun    old = (ofun) iCmpFun;
  29.     iCmpFun = fun;
  30.     return old;
  31. }
  32.  
  33. cmeth    gNew()
  34. {
  35.     object    obj = gNew(super);
  36.     accessIVsOf(obj);
  37.     INITIALIZECRITICALSECTION(iCS);
  38.     return obj;
  39. }
  40.  
  41. imeth    gDeepDispose()
  42. {
  43.     ENTERCRITICALSECTION(iCS);
  44.     if (iNodes)
  45.         gDeepDispose(iNodes);
  46.     LEAVECRITICALSECTION(iCS);
  47.     DELETECRITICALSECTION(iCS);
  48.     return gDispose(super);
  49. }
  50.  
  51. imeth    gDispose()
  52. {
  53.     ENTERCRITICALSECTION(iCS);
  54.     if (iNodes)
  55.         gDispose(iNodes);
  56.     LEAVECRITICALSECTION(iCS);
  57.     DELETECRITICALSECTION(iCS);
  58.     return gDispose(super);
  59. }
  60.  
  61. imeth    gDeepDisposeAllNodes()
  62. {
  63.     ENTERCRITICALSECTION(iCS);
  64.     if (iNodes)
  65.         iNodes = gDeepDispose(iNodes);
  66.     iNum = 0;
  67.     LEAVECRITICALSECTION(iCS);
  68.     return self;
  69. }
  70.  
  71. imeth    gDisposeAllNodes()
  72. {
  73.     ENTERCRITICALSECTION(iCS);
  74.     if (iNodes)
  75.         iNodes = gDispose(iNodes);
  76.     iNum = 0;
  77.     LEAVECRITICALSECTION(iCS);
  78.     return self;
  79. }
  80.  
  81. imeth    int    gSize()
  82. {
  83.     return iNum;
  84. }
  85.  
  86. imeth    gSetTopNode(new)
  87. {
  88.     iNodes = new;
  89.     return self;
  90. }
  91.  
  92. imeth    gAddValue(key, val)
  93. {
  94.     object    old=NULL;
  95.     int    replaced;
  96.     
  97.     ENTERCRITICALSECTION(iCS);
  98.     if (!iNodes) {
  99.         if (!iCmpFun)
  100.             iCmpFun = gCompare;
  101.         iNodes = gNewNode(BTreeNode, self, 2);
  102.     }
  103.     gAddBTreeNode(iNodes, iCmpFun, key, val, 1, &replaced, NULL, &old);
  104.     if (replaced == 1)
  105.         iNum++;
  106.     LEAVECRITICALSECTION(iCS);
  107.     return old;
  108. }
  109.  
  110. imeth    gFindEQ(key, object *foundKey)
  111. {
  112.     object    r;
  113.     ENTERCRITICALSECTION(iCS);
  114.     r = iNodes && iNum ? gFindBTNEQ(iNodes, iCmpFun, key, foundKey) : NULLOBJ;
  115.     LEAVECRITICALSECTION(iCS);
  116.     return r;
  117. }
  118.  
  119. imeth    gFindGE(key, object *foundKey)
  120. {
  121.     object    r;
  122.     ENTERCRITICALSECTION(iCS);
  123.     r = iNodes && iNum ? gFindBTNGE(iNodes, iCmpFun, key, foundKey) : NULLOBJ;
  124.     LEAVECRITICALSECTION(iCS);
  125.     return r;
  126. }
  127.  
  128. imeth    gFindGT(key, object *foundKey)
  129. {
  130.     object    r;
  131.     ENTERCRITICALSECTION(iCS);
  132.     r = iNodes && iNum ? gFindBTNGT(iNodes, iCmpFun, key, foundKey) : NULLOBJ;
  133.     LEAVECRITICALSECTION(iCS);
  134.     return r;
  135. }
  136.  
  137. imeth    gFindNext(object *foundKey)
  138. {
  139.     object    r;
  140.     ENTERCRITICALSECTION(iCS);
  141.     if (*foundKey) {
  142.         object    key = *foundKey;
  143.         r = gFindBTNGT(iNodes, iCmpFun, key, foundKey);
  144.     } else
  145.         r = gFindFirst(self, foundKey);
  146.     LEAVECRITICALSECTION(iCS);
  147.     return r;
  148. }
  149.  
  150. imeth    gFindLE(key, object *foundKey)
  151. {
  152.     object    r;
  153.     ENTERCRITICALSECTION(iCS);
  154.     r = iNodes && iNum ? gFindBTNLE(iNodes, iCmpFun, key, foundKey) : NULLOBJ;
  155.     LEAVECRITICALSECTION(iCS);
  156.     return r;
  157. }
  158.  
  159. imeth    gFindLT(key, object *foundKey)
  160. {
  161.     object    r;
  162.     ENTERCRITICALSECTION(iCS);
  163.     r = iNodes && iNum ? gFindBTNLT(iNodes, iCmpFun, key, foundKey) : NULLOBJ;
  164.     LEAVECRITICALSECTION(iCS);
  165.     return r;
  166. }
  167.  
  168. imeth    gFindPrev(object *foundKey)
  169. {
  170.     object    r;
  171.     ENTERCRITICALSECTION(iCS);
  172.     if (*foundKey) {
  173.         object    key = *foundKey;
  174.         r = gFindBTNLT(iNodes, iCmpFun, key, foundKey);
  175.     } else
  176.         r = gFindLast(self, foundKey);
  177.     LEAVECRITICALSECTION(iCS);
  178.     return r;
  179. }
  180.  
  181. imeth    gFindFirst(object *foundKey)
  182. {
  183.     object    r;
  184.     ENTERCRITICALSECTION(iCS);
  185.     r = iNodes && iNum ? gFindBTNFirst(iNodes, iCmpFun, foundKey) : NULLOBJ;
  186.     LEAVECRITICALSECTION(iCS);
  187.     return r;
  188. }
  189.  
  190. imeth    gFindLast(object *foundKey)
  191. {
  192.     object    r;
  193.     ENTERCRITICALSECTION(iCS);
  194.     r = iNodes && iNum ? gFindBTNLast(iNodes, iCmpFun, foundKey) : NULLOBJ;
  195.     LEAVECRITICALSECTION(iCS);
  196.     return r;
  197. }
  198.  
  199. imeth    gDisposeObj(key)
  200. {
  201.     object    res;
  202.     ENTERCRITICALSECTION(iCS);
  203.     res = iNodes && iNum ? gDeleteBTNode(iNodes, iCmpFun, key, 0, NULL) : NULL;
  204.     if (res)
  205.         iNum--;
  206.     LEAVECRITICALSECTION(iCS);
  207.     return res;
  208. }
  209.  
  210. imeth    gDeepDisposeObj(key)
  211. {
  212.     object    res;
  213.     ENTERCRITICALSECTION(iCS);
  214.     res = iNodes && iNum ? gDeleteBTNode(iNodes, iCmpFun, key, 1, NULL) : NULL;
  215.     if (res)
  216.         iNum--;
  217.     LEAVECRITICALSECTION(iCS);
  218.     return res;
  219. }
  220.  
  221. imeth    gPrint(stream)
  222. {
  223.     ENTERCRITICALSECTION(iCS);
  224.     vPrintf(stream, "BTree [%8.8lx], %ld keys, first node is %8.8lx\n", self, iNum, iNodes);
  225.     if (iNodes) {
  226.         gPuts(stream, "\n------------------------------------------------------------\n");
  227.         gPrint(iNodes, stream);
  228.     }
  229.     LEAVECRITICALSECTION(iCS);
  230.     return self;
  231. }
  232.  
  233.  
  234. /*                                      
  235.  *
  236.  *      Copyright (c) 1993-1996 Algorithms Corporation
  237.  *      3020 Liberty Hills Drive
  238.  *      Franklin, TN 37067
  239.  *
  240.  *      ALL RIGHTS RESERVED.
  241.  *
  242.  *      
  243.  *      
  244.  */
  245.